package com.example.day_0813_103.slice;

import com.example.day_0813_103.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.HashSet;
import java.util.Set;

public class Two_AbilitySlice extends AbilitySlice {
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP,0x00201,"MY_TAG");

    @Override
    public void onStart(Intent intent) {
        HiLog.info(LABEL,"这里是 Two_AbilitySlice 的 onStart");
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_two_);

//      跳转到 Three_Ability
        findComponentById(ResourceTable.Id_Two_id)
                .setClickedListener(com -> myAblit("com.example.day_0813_103.Three_Ability"));
//      跳转到 Two_AbilitySlice_1 无传值
        findComponentById(ResourceTable.Id_Two_Two)
                .setClickedListener(component -> present(new Two_AbilitySlice_1(),new Intent()));
//      再次跳转Two_AbilitySlice_1 的第二种方法  有传值  有回调
        findComponentById(ResourceTable.Id_Two_three)
                .setClickedListener(component -> presentForResult(new Two_AbilitySlice_1(),new Intent().setParam("name","冲冲冲"),100));
//      跳转到 同一个Ability 的第二个 Slice  （Three_AbilitySlice 和 Three_AbilitySlice2）
        findComponentById(ResourceTable.Id_Two_four)
                .setClickedListener(component -> myablity("com.example.day_0813_103.Three_Ability"));
//      跳转到 Five_AbilitySlice   无传值
        findComponentById(ResourceTable.Id_Two_five)
                .setClickedListener(component -> FiveAblity("com.example.day_0813_103.Five_Ability"));
    }

    public void FiveAblity(String component) {
        Intent it = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("com.example.day_0813_103")
                .withAbilityName(component)
                .build();
        it.setOperation(operation);
        startAbility(it);
    }
    public void myablity(String component) {
        Intent it = new Intent();
        it.setAction("Two_three");
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("com.example.day_0813_103")
                .withAbilityName(component)
                .build();
        it.setOperation(operation);
        startAbility(it);
    }

    private void myAblit(String abilityName) {
        Intent it = new Intent();
        Set<String> set = new HashSet<>();
        set.add("顺利");
        set.add("执行");
        set.add("帅气");
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("com.example.day_0813_103")
                .withAbilityName(abilityName)
                .withEntities(set)
                .build();
        it.setOperation(operation);
        it.setParam("selut","luky");
        startAbilityForResult(it,111);
    }

    // Two_AbilitySlice_1  setResult 的回调
    @Override
    protected void onResult(int requestCode, Intent resultIntent) {
        super.onResult(requestCode, resultIntent);
        HiLog.info(LABEL,String.valueOf(requestCode));
        HiLog.info(LABEL,resultIntent.getStringParam("back"));
    }

    @Override
    public void onActive() {
        HiLog.info(LABEL,"这里是 Two_AbilitySlice 的 onActive");
        super.onActive();
    }
    @Override
    protected void onInactive() {
        HiLog.info(LABEL,"这里是 Two_AbilitySlice 的 onInactive");
        super.onInactive();
    }

    @Override
    protected void onBackground() {
        HiLog.info(LABEL,"这里是 Two_AbilitySlice 的 onBackground");
        super.onBackground();
    }

    @Override
    public void onForeground(Intent intent) {
        HiLog.info(LABEL,"这里是 Two_AbilitySlice 的 onForeground");
        super.onForeground(intent);
    }
    @Override
    protected void onStop() {
        HiLog.info(LABEL,"这里是 Two_AbilitySlice 的 onStop");
        super.onStop();
    }
}
